
export const state = () => ({
  tabIndex: 0,
  queryParam: [
    { pageNum: 1, pageSize: 10, type: 1, code: undefined, name: undefined },
    { pageNum: 1, pageSize: 10, type: 2, code: undefined, name: undefined },
    { pageNum: -1, area_pid: undefined, area_id: undefined },
    { pageNum: -1, firId: undefined, secId: undefined }
  ],

  planQueryParam: {
    pageNum: 1,
    pageSize: 10,
    name: undefined
  },

  warehouseQueryParam: {
    pageNum: 1,
    pageSize: 10,
    stockType: undefined,
    status: undefined,
    userName: undefined,
    approverName: undefined
  },

  applyQueryParam: [
    { pageNum: 1, pageSize: 10, applyType: '申购单', sn: undefined, createdTime: undefined, status: undefined, deptId: undefined, userName: undefined },
    { pageNum: 1, pageSize: 10, applyType: '申领单', sn: undefined, createdTime: undefined, status: undefined, deptId: undefined, userName: undefined },
    { pageNum: 1, pageSize: 10, applyType: '报修单', sn: undefined, createdTime: undefined, status: undefined, attr: undefined, location: undefined }
  ],

  itemFirTypeList: [],
  itemSecTypeListMaps: {}, // firId: [secType, secType, ...]
  itemFirTypeMaps: {},
  itemSecTypeMaps: {},
  itemMaps: {},
  itemListMaps: {} // secId: [item, item, ...]
})

export const mutations = {
  SET_ITEM_TYPES (state, list = []) {
    state.itemFirTypeMaps = {}
    state.itemSecTypeMaps = {}
    state.itemSecTypeListMaps = {}
    const { itemFirTypeList, itemSecTypeMaps, itemSecTypeListMaps, itemFirTypeMaps } = state
    itemFirTypeList.splice(0)
    list.forEach(typeItem => {
      const { pid, id } = typeItem
      if (pid > -1) {
        if (!itemSecTypeListMaps[pid]) itemSecTypeListMaps[pid] = []
        itemSecTypeListMaps[pid].push(typeItem)
        itemSecTypeMaps[id] = typeItem
      } else {
        itemFirTypeList.push(typeItem)
        itemFirTypeMaps[id] = typeItem
      }
    })
    itemFirTypeList.forEach(item => {
      const children = this.$get(itemSecTypeListMaps, item.id, [])
      item.children = children
    })
  },

  SET_ITEM (state, list = []) {
    const maps = {}
    const itemMaps = {}
    list.forEach(item => {
      if (!maps[item.secId]) maps[item.secId] = []
      maps[item.secId].push(item)
      itemMaps[item.id] = item
    })
    state.itemListMaps = maps
    state.itemMaps = itemMaps
  },

  TAB_CHANGE (state, { name = '', value = 0 }) {
    if (!name) state.tabIndex = value
    else state[name].tabIndex = value
  },

  SET_QUERY_PARAM (state, { target = {}, value = 0 }) {
    Object.assign(target, value)
  }
}

export const actions = {
  getItemTypes ({ commit }) {
    return this.$axios.post('api/assetsCategory/query', { pageNum: -1 }).then(res => {
      commit('SET_ITEM_TYPES', this.$get(res, 'data.data.dataList'))
    })
  },

  getItems ({ commit }) {
    return this.$axios.post('api/assetsGoods/query', { pageNum: -1 }).then(res => {
      commit('SET_ITEM', this.$get(res, 'data.data.dataList'))
    })
  },

  tabChange ({ commit }, params) {
    commit('TAB_CHANGE', params)
  },

  setQueryParam ({ commit }, params) {
    commit('SET_QUERY_PARAM', params)
  }
}
